package com.example.sjly.db;

import android.app.Application;

import com.example.sjly.sqliteroom.AppRoomDatabase;
import com.example.sjly.sqliteroom.User;

//使用饿汉模式实现单例的登录用户信息记录
//但是此处是存储用户信息的副本，这点是否合适有待考虑，可以考虑使用User类
//LoginUser相对于模拟登陆，并且作为存储数据库的一个缓冲区
//或许可作为全局变量
public class LoginUser extends Application {
    private static LoginUser loginUser;
    private static User _user;
    private int id;
    private String name;
    private String username;
    private byte[] portrait;//头像
    private String region;
    private String sex;
    private String age;

    @Override
    public void onCreate() {
        super.onCreate();
        loginUser = this;
    }
    public static LoginUser getInstance()
    {
        return loginUser;
    }

    public LoginUser() {
    }



    public User getUser(){
        return _user;
    }

    //保存至数据库
    public void update(){
        if (_user.getId()==this.id) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    AppRoomDatabase db=AppRoomDatabase.getInstance(getApplicationContext());
                    _user=db.userdao().getByName(loginUser.username);
                }
            }).start();
            _user.setName(this.name);
            _user.setPortrait(this.portrait);
            _user.setSex(this.sex);
            _user.setRegion(this.region);
            _user.setAge(this.age);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    AppRoomDatabase db=AppRoomDatabase.getInstance(getApplicationContext());
                    db.userdao().updateUsers(_user);
                }
            }).start();

        }
    }

    //重新init
    public void reinit(){
        loginUser.id = _user.getId();
        loginUser.name = _user.getName();
        loginUser.portrait = _user.getPortrait();
        loginUser.region = _user.getRegion();
        loginUser.sex = _user.getSex();
        loginUser.age = _user.getAge();
    }

    public void login(User user) {
        _user = user;//user有值
        loginUser.id = user.getId();
        loginUser.username=user.getUsername();
        loginUser.name = user.getName();
       loginUser.portrait = user.getPortrait();
        loginUser.region = user.getRegion();
        loginUser.sex = user.getSex();
        loginUser.age = user.getAge();
    }

    public boolean logout(){
        if(loginUser.id == -1) return false;
        loginUser.id = -1;
        loginUser.name = null;
        loginUser.portrait = null;
        loginUser.region = null;
        loginUser.sex = null;
        loginUser.age = null;
        return true;
    }


    @Override
    public String toString() {
        return "LoginUser{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", portrait ='" + portrait + '\'' +
                ", region='" + region + '\'' +
                ", gender='" + sex + '\'' +
                ", brithday='" + age + '\'' +
                '}';
    }
    public static User get_user() {
        return _user;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public static void set_user(User _user) {
        LoginUser._user = _user;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getPortrait() {
        return portrait;
    }

    public void setPortrait(byte[] portrait) {
        this.portrait = portrait;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getGender() {
        return sex;
    }

    public void setGender(String gender) {
        this.sex = gender;
    }

    public String getBrithday() {
        return age;
    }

    public void setBrithday(String brithday) {
        this.age = brithday;
    }
}
